home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 7: Sunsite / Linux Cubed Series 7 - Sunsite Vol 1.iso / system / admin / linuxcon.000 / linuxcon / linuxconf-1.6 / netconf / daemon1.c < prev    next >
C/C++ Source or Header  |  1996-08-01  |  7KB  |  280 lines

  1. #include <stdlib.h>
  2. #include <string.h>
  3. #include <ctype.h>
  4. #include <limits.h>
  5. #include "netconf.h"
  6. #include "../misc/misc.h"
  7. #include "../xconf/xconf.h"
  8. #include "netconf.m"
  9.  
  10. static NETCONF_HELP_FILE helpf ("scope");
  11. static CONFIG_FILE f_usrconf_daemons (USR_LIB_CONF_DAEMONS,helpf,CONFIGF_NONE);
  12.  
  13. static DAEMON *first = NULL;
  14. static char loaded = 0;
  15.  
  16. /*
  17.     Read a configuration file about command and path named (conf.daemons)
  18. */
  19. static int daemon_read(CONFIG_FILE &cf)
  20. {
  21.     FILE *fin = cf.fopen ("r");
  22.     int nbdae = -1;
  23.     if (fin != NULL){
  24.         nbdae = 0;
  25.         char buf[2000];
  26.         while (fgets(buf,sizeof(buf)-1,fin)!=NULL){
  27.             char nbuf[2000];
  28.             str_strip(buf,nbuf);
  29.             char *pt = str_skip(nbuf);
  30.             if (*pt != '\0'){
  31.                 /* #Specification: files / ETC_CONF_DAEMONS / format
  32.                     This file has one line per daemon described.
  33.  
  34.                     If the first character of the line is a !, it means
  35.                     that netconf won't start/stop this daemon. This
  36.                     character is optionnal. The rest of the line goes like
  37.                     this.
  38.  
  39.                     (Note that this feature has been pushed
  40.                     in the file /etc/conf.linuxconf. This
  41.                     mean that the conf.daemons files
  42.                     are never written by linuxconf. The
  43.                     feature is kept for compatibility).
  44.  
  45.                     The first field is the name (not the path) of the
  46.                     daemon. The next field is the full path and the
  47.                     rest of the line is the command line arguments
  48.                     to use to start the daemon.
  49.  
  50.                     For now, there is no comments, nor continuation line.
  51.                     Empty line are supported.
  52.  
  53.                     This file is useful to specify path, so it is used
  54.                     to control other commands, not just daemons.
  55.                 */
  56.                 int managed = 1;
  57.                 if (*pt == '!'){
  58.                     pt = str_skip(pt+1);
  59.                     managed = 0;
  60.                 }
  61.                 char name[100];
  62.                 char *ptn = name;
  63.                 while (*pt > ' ') *ptn++ = *pt++;
  64.                 *ptn = '\0';
  65.                 while (isspace(*pt)) pt++;
  66.                 DAEMON *dae = daemon_new (managed,name,pt,first);
  67.                 if (dae != NULL && dae->isok()){
  68.                     first = dae;
  69.                     nbdae++;
  70.                 }else{
  71.                     delete dae;
  72.                 }
  73.             }
  74.         }
  75.         fclose (fin);
  76.     }
  77.     return nbdae;
  78. }
  79.  
  80. /*
  81.     Register a daemon or command.
  82.     This is generally used by modules
  83. */
  84. void daemon_register (DAEMON *dae, const char *name, const char *buf)
  85. {
  86.     dae->init (1,name,buf,first);
  87.     first = dae;
  88. }
  89.  
  90. static const char DAEMONS[] = "daemons";
  91. static const char NOTMANAGED[] = "notmanaged";
  92. static const char CMDSPEC[] = "cmdspec";
  93. /*
  94.     Read information about daemons and commands.
  95. */
  96. static int daemon_read()
  97. {
  98.     /* #Specification: conf.daemons / override
  99.         The content of /usr/lib/linuxconf/conf.daemons can be overriden
  100.         by the administrator. The override values (path and argument)
  101.         are stored in /etc/conf.linuxconf.
  102.     */
  103.     loaded = 1;
  104.     int nbdae = daemon_read (f_usrconf_daemons);
  105.     {
  106.         SSTRINGS lst;
  107.         linuxconf_getall (DAEMONS,NOTMANAGED,lst,0);
  108.         int nb = lst.getnb();
  109.         for (int i=0; i<nb; i++){
  110.             SSTRING *s = lst.getitem(i);
  111.             const char *pt = s->get();
  112.             DAEMON *next = first;
  113.             while (next != NULL){
  114.                 if (strcmp(pt,next->getname())==0){
  115.                     next->set_managed(0);
  116.                     break;
  117.                 }
  118.                 next = next->getnext();
  119.             }
  120.         }
  121.     }
  122.     {
  123.         SSTRINGS lst;
  124.         linuxconf_getall (DAEMONS,CMDSPEC,lst,0);
  125.         int nb = lst.getnb();
  126.         for (int i=0; i<nb; i++){
  127.             SSTRING *s = lst.getitem(i);
  128.             char name[PATH_MAX];
  129.             char path[PATH_MAX];
  130.             char *pt = str_copyword (name,s->get());
  131.             pt = str_copyword (path,pt);
  132.             pt = str_skip(pt);
  133.             DAEMON *next = first;
  134.             while (next != NULL){
  135.                 if (strcmp(name,next->getname())==0){
  136.                     if (strcmp(path,next->getpath())!=0
  137.                         || strcmp(pt,next->getargs())!=0){
  138.                         next->set_override (1);
  139.                         next->setspec (path,pt);
  140.                     }
  141.                     break;
  142.                 }
  143.                 next = next->getnext();
  144.             }
  145.         }
  146.     }
  147.     return nbdae;
  148. }
  149.  
  150.  
  151. static void daemon_write()
  152. {
  153.     linuxconf_removeall (DAEMONS,NOTMANAGED);
  154.     linuxconf_removeall (DAEMONS,CMDSPEC);
  155.     DAEMON *next = first;
  156.     while (next != NULL){
  157.         if (!next->is_managed()){
  158.             linuxconf_add (DAEMONS,NOTMANAGED,next->getname());
  159.         }
  160.         if (next->is_overriden()){
  161.             char spec[3*PATH_MAX];
  162.             sprintf (spec,"%s %s %s",next->getname(),next->getpath()
  163.                 ,next->getargs());
  164.             linuxconf_add (DAEMONS,CMDSPEC,spec);
  165.         }
  166.         next = next->getnext();
  167.     }
  168.     linuxconf_save();
  169. }
  170. /*
  171.     Check if a process is running at least once
  172.     Return -1 or the start time of this process.
  173. */
  174. DAEMON *daemon_find (const char *name)
  175. {
  176.     if (!loaded) daemon_read ();
  177.     DAEMON *ret = first;
  178.     while (ret != NULL){
  179.         if (strcmp(name,ret->getname())==0){
  180.             break;
  181.         }else{
  182.             ret = ret->getnext();
  183.         }
  184.     }
  185.     if (ret == NULL){
  186.         xconf_error (
  187.             MSG_U(E_MISSINFO
  188.              ,"Missing information about daemon or utility %s\n"
  189.              "in configuration file %s\n")
  190.             ,name,USR_LIB_CONF_DAEMONS);
  191.     }
  192.     return ret;
  193. }
  194.  
  195. static int daemon_cmp(const void *pt1, const void *pt2)
  196. {
  197.     DAEMON *p1 = *(DAEMON**)pt1;
  198.     DAEMON *p2 = *(DAEMON**)pt2;
  199.     return strcmp(p1->getname(),p2->getname());
  200. }
  201.  
  202. /*
  203.     Edit the path and arguments of a daemon or command
  204. */
  205. PUBLIC int DAEMON::edit ()
  206. {
  207.     int ret = -1;
  208.     DIALOG dia;
  209.     SSTRING epath(path);
  210.     SSTRING eargs(args);
  211.     dia.newf_str (MSG_U(F_DAEPATH,"path of the command"),epath);
  212.     dia.newf_str (MSG_U(F_DAEARGS,"arguments"),eargs);
  213.     if (dia.edit (MSG_U(T_DAECONFIG,"Daemons and command config")
  214.         ,MSG_U(I_DAECONFIG,"You are allowed to changed the way\n"
  215.             "a daemon or command is invoked.")
  216.         ,helpf,0)==MENU_ACCEPT){
  217.         if (epath.cmp(path)!=0 || eargs.cmp(args)!=0){
  218.             setspec (epath.get(),eargs.get());
  219.             set_override(1);
  220.         }
  221.         ret = 0;
  222.     }
  223.     return ret;
  224. }
  225.  
  226. /*
  227.     Let the user select which daemon or command netconf may operate
  228. */
  229. void daemon_config()
  230. {
  231.     DAEMON *tb[100];
  232.     int nb = 0;
  233.     if (!loaded) daemon_read();
  234.     {
  235.         DAEMON *next = first;
  236.         while (next != NULL){
  237.             if (next->isok()) tb[nb++] = next;
  238.             next = next->getnext();
  239.         }
  240.         qsort (tb,nb,sizeof(DAEMON*),daemon_cmp);
  241.         DIALOG dia;
  242.         char status[100];
  243.         for (int i=0; i<nb; i++){
  244.             DAEMON *dae = tb[i];
  245.             status[i] = dae->is_managed();
  246.             char buf[PATH_MAX];
  247.             sprintf (buf,"%s%s",dae->getname()
  248.                 ,dae->is_overriden() ? " (*)" : "");
  249.             dia.newf_chk ("",status[i],buf);
  250.         }
  251.         int nof = 0;
  252.         while (1){
  253.             MENU_STATUS code = dia.edit (
  254.                 MSG_U(T_SCOPE,"Netconf scope")
  255.                 ,MSG_U(I_SCOPE,"This dialog let you select which commands\n"
  256.                  "and daemon netconf is allowed to managed\n"
  257.                  "If you deselect one command, then you must\n"
  258.                  "managed this yourself. This facility\n"
  259.                  "is provided to help integrate netconf into\n"
  260.                  "a working setup.\n"
  261.                  "*** Unless you know what you are doing ***\n"
  262.                  "*** fiddling here is not recommended   ***\n")
  263.                 ,helpf
  264.                 ,nof,MENUBUT_ACCEPT|MENUBUT_CANCEL|MENUBUT_EDIT);
  265.             if (code == MENU_ACCEPT){
  266.                 for (i=0; i<nb; i++){
  267.                     tb[i]->set_managed(status[i]);
  268.                 }
  269.                 daemon_write();
  270.                 break;
  271.             }else if (code == MENU_EDIT){
  272.                 if (tb[nof]->edit() != -1) daemon_write();
  273.             }else{
  274.                 break;
  275.             }
  276.         }
  277.     }
  278. }
  279.  
  280.